import { api } from "../utils/request";
import type { CreateUserParams, LoginUserParams, Params, UploadUser, UserInfo, UserInfoS } from "./api-types";


// 定义响应数据结构
interface Captcha {
  token: string;
  svg: string
}

interface VerifyCaptcha {
  captchaId: string;
  code: string
}

interface FIleData{
  originalName: string
  fileName: string
  path: string
  size: number
}

// 获得验证码
export const getCaptchaApi =  async () => {
  const result = await api.get<Captcha>(`/users/captcha`);
  return result;
}

// 验证验证码
export const postverifyCaptchaApi = async (verifyCaptcha: VerifyCaptcha) => {
  const result = await api.post<true, VerifyCaptcha>(`/users/verify-captcha`, verifyCaptcha);
  return result
}

//注册
export const postCreateUserApi = async (createUser: CreateUserParams) => {
  const result = await api.post<string, CreateUserParams>(`/users/create`, createUser);
  return result
}

// 登录
export const postLoginApi = async (loginUser: LoginUserParams) => {
  const result = await api.post<UserInfo, LoginUserParams>(`/users/login`, loginUser);
  return result
}

// 文件上传接口
export const postUploadApi = async (fileParams: FormData) => {
  const result = await api.post<FIleData, FormData>('/upload', fileParams, { headers: { 'Content-Type': 'multipart/form-data' }})
  return result
}

// 修改用户信息
export const postUploadUserApi = async (uploadUser: UploadUser) => {
  const result = await api.post<UploadUser, UploadUser>('/users/upload', uploadUser)
  return result
}

// 获得所有的用户
export const getUsersApi = async (params: Params) => {
  const result = await api.get<UserInfoS[], Params>('/users/users', params)
  return result
}

// 删除用户
export const deleteUserApi = async (id: string) => {
  const result = await api.delete<string, string>(`/users/delete/${id}`)
  return result
}